﻿using BLL;
using DAL;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using WeChat.WxPayAPI.WeHelper;

namespace WeChat.Ajax
{
    /// <summary>
    /// Warte 的摘要说明
    /// </summary>
    public class Warte : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            JObject jo = new JObject();

            string funcType = context.Request.Form["funcType"];
            string wcmodelname = context.Request.Form["wcmodelname"];
            

            //获取Opid
            string openid = context.Session["openid"] == null ? "" : context.Session["openid"].ToString();
            DataTable data = BindIdBLL.getryxx(openid);
            DataTable sysset = BindIdBLL.systemset();
            string Rtung = "";
            switch (funcType) 
            {
                case "weconfig":
                    string appid = WeHelper.appId;
                    string timpstamp = WeHelper.CreateTimestamp();
                    string nonceStr = WeHelper.CreateNonceStr();
                    string url =context.Request["wereurl"];
                    string sig = WeHelper.GetSignature(timpstamp, nonceStr, HttpUtility.UrlDecode(url));
                    ApplicationLog.Write("sig:"+sig+ "url:"+ HttpUtility.UrlDecode(url));
                    var weconfigList = new { appid= appid,timpstamp = timpstamp, nonceStr= nonceStr, sig= sig };
                    Rtung = JsonConvert.SerializeObject(weconfigList);
                    break;
                case "UserInfo":
                    jo.Add("systemset", JsonConvert.SerializeObject(BindIdBLL.systemset()));
                    jo.Add("user", JsonConvert.SerializeObject(BindIdBLL.getryxx(openid)));
                    Rtung = jo.ToString();
                    break;
                case "wkstop":
                    string jh = context.Request["equiment"];
                    string carnumber = data.Rows[0]["rfkh"].ToString();
                    Rtung =BindIdBLL.wkstate_update(carnumber,Convert.ToInt32(jh), 5).ToString();
                    break;
                case "workstate":
                    Rtung = "-1";

                    if(data.Rows.Count > 0) 
                    {
                        jh =  context.Request["equiment"];
                        carnumber = data.Rows[0]["rfkh"].ToString();
                        DataTable wkstate = BindIdBLL.wkstate_lssj(carnumber);
                        if (wkstate.Rows.Count > 0) 
                        {
                            int wtstate =Convert.ToInt32(wkstate.Rows[0]["waterstate"]);
                            Rtung = wtstate.ToString()+"id="+ wkstate.Rows[0]["sn"] + "&jh=" + wkstate.Rows[0]["xfjh"];
                            if (Rtung == "3" && wkstate.Rows.Count > 0)
                            {
                                DateTime wktime = Convert.ToDateTime(wkstate.Rows[0]["cjsj"]);
                                int waterunit = Convert.ToInt32(wkstate.Rows[0]["waterunit"])+6;
                                if (DateTime.Now.AddSeconds(-waterunit) > wktime)
                                {
                                    Rtung = "-1";
                                }
                            }
                        }

                    }

                        break;
                case "btsubmit":
                    if (data.Rows.Count > 0) 
                    {
                        int wcmodel =Convert.ToInt32(sysset.Rows[0]["wcmodel"]);
                        int timing = Convert.ToInt32(sysset.Rows[0]["timing"]);
                        double timingmoney = Convert.ToDouble(sysset.Rows[0]["timingmoney"]);
                        int measurement = Convert.ToInt32(sysset.Rows[0]["measurement"]);
                        double measurementmoney = Convert.ToDouble(sysset.Rows[0]["measurementmoney"]);
                        double price =Convert.ToDouble(context.Request["price"]);
                        int pricenum = Convert.ToInt32(context.Request["price"]);
                        if (wcmodel == 0) //计时费率
                        {
                            price = (timingmoney / 100) * price / (timing);
                        }
                        if (wcmodel == 1)//计量费率
                        {
                            price = (measurementmoney/100)* price/ (measurement);
                        }
                        
                        string ordernumber = DateTime.Now.ToString("yyyyMMddHHmmssffff")+new Random().Next(999)+"SK";
                        string machinenumber = context.Request["machinenumber"];
                        jh = context.Request["equiment"];
                        carnumber = data.Rows[0]["rfkh"].ToString();                      
                        double rfye =Convert.ToDouble(data.Rows[0]["rfye"]);
                        string rfzt = data.Rows[0]["rfzt"].ToString();
                        if (rfzt !="32") 
                        {
                            Rtung = "-4";//卡状态非正常卡
                            break;
                        }
                        if (rfye >= price)
                        {
                            //水控消费
                            int wartenum = BindIdBLL.warteIn(machinenumber, carnumber,Convert.ToDouble(price.ToString("0.00")), jh, "00", "水控模式", wcmodelname, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "二维码消费", "", pricenum, 0, ordernumber);
                            if (wartenum > 0)
                            {
                                Rtung = "1";
                            }
                            else
                            {
                                Rtung = "-2";
                            }
                        }
                        else 
                        {
                            Rtung = "-3";//卡余额不足;
                        }
                    }
                    break;
            }
                context.Response.Write(Rtung);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}